package com.example.springboot_jap_model.controller;

import com.example.springboot_jap_model.domain.Authority;
import com.example.springboot_jap_model.domain.RespEntity;
import com.example.springboot_jap_model.service.AuthService;
import com.example.springboot_jap_model.utils.PageableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;
    private final static Logger logger = LoggerFactory.getLogger(AuthController.class);

    /**
     * 跳转 到 保存 auth 页面
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("hopeAuth")
    public String hopeAuth(ModelMap modelMap) throws JSONException {
        Pageable pageable = PageableUtils.getPageable(0);
        authService.findAllAuth(modelMap,pageable);
        return "/main/auth/authPage";
    }
    @PostMapping("testAuth")
    @ResponseBody
    public RespEntity testAuth(ModelMap modelMap) throws JSONException {

        return authService.findAllAuth(modelMap);
    }

    /**
     * 保存 权限
     *
     * @param authority
     * @return
     */
    @PostMapping("addAuth")
    @ResponseBody
    public RespEntity addAuth(Authority authority) {
        logger.info("---------------- 保存 权限 ---------------------");
        return authService.addAuthority(authority);
    }

    @RequestMapping("hopeAuthGroup")
    public String getAuthGroup(ModelMap modelMap) throws JSONException {
        authService.findAllAuth(modelMap, PageableUtils.getPageable(0));
        return "/main/auth/authGroupPage";
    }

    @RequestMapping("authGroup")
    @ResponseBody
    public RespEntity authGroup(@RequestParam("authId") String authId, @RequestParam("roleId") Long roleId) {

        return authService.generateRole(roleId, authId);
    }


}
